const { getAllTypes, getAllShopsBySearch, deleteShopbyId, addShop, updateShopbyUserId,getShopbyId } = require("../services/shopService");
const fs = require('fs')
const path = require('path')

module.exports = {
    getAllTypes: async (ctx, next) => {
        try {
            let types = await getAllTypes();
            if (types) {
                ctx.body = {
                    success: true,
                    data: { types: types },
                    info: { msg: "获取商品类别成功" },
                    code: 200
                }
            } else {
                ctx.body = {
                    success: false,
                    data: { types: types },
                    info: { msg: "获取商品类别失败" },
                    code: 200
                }
            }
        } catch (error) {
            ctx.body = {
                success: false,
                data: {},
                info: { msg: "服务器请求异常" },
                code: 500
            }
        }
    },
    getSearchShopManage: async (ctx, next) => {
        try {
            console.log(ctx.request.body);
            const { id, shopname, shoptype } = ctx.request.body;
            console.log(id, shopname, shoptype);
            let seachResult = await getAllShopsBySearch(id, shopname, shoptype);
            if (seachResult) {
                ctx.body = {
                    success: true,
                    data: { seachResult: seachResult },
                    info: { msg: "获取商品列表成功" },
                    code: 200
                }
            } else {
                ctx.body = {
                    success: false,
                    data: { seachResult: seachResult },
                    info: { msg: "获取商品列表失败" },
                    code: 200
                }
            }
        } catch (error) {
            ctx.body = {
                success: false,
                data: {},
                info: { msg: "服务器请求异常" },
                code: 500
            }
        }
    },
    addShop: async (ctx, next) => {
        try {
            const file = ctx.request.files.file;
            const fileData = fs.readFileSync(file.filepath);
            const shop = ctx.request.body;
            const { shopid, shopName, type, price } = ctx.request.body;
            console.log(shopid, shopName, type, price);
            const shopPicFileName = shopid + '.'
                + file.originalFilename.substring(file.originalFilename.lastIndexOf('.') + 1);
            console.log("shopPicFileName", shopPicFileName);
            let filePath = path.join(process.cwd(), '/public/upload/shop/') + `${shopPicFileName}`;
            console.log("filePath", filePath);//路径
            let seachResult = await getShopbyId(shopid);
            if (seachResult) {
                ctx.body = {
                    success: false,
                    data: {},
                    info: { msg: "该编号已存在" },
                    code: 200
                }
            } else {
                let addResult = await addShop(shopid, shopName, type, filePath, price);
                if (addResult) {
                    fs.writeFileSync(filePath, fileData); //filePath 写入文件的路径；fileData 写入文件的内容   
                    ctx.body = {
                        success: true,
                        data: { addResult: addResult },
                        info: { msg: "添加商品成功" },
                        code: 200
                    }
                } else {
                    ctx.body = {
                        success: false,
                        data: { addResult: addResult },
                        info: { msg: "添加商品失败" },
                        code: 200
                    }
                }
            }
        } catch (error) {
            ctx.body = {
                success: false,
                data: {},
                info: { msg: "服务器请求异常" },
                code: 500
            }
        }
    },
    deleteShop: async (ctx, next) => {
        try {
            console.log(ctx.params.id);
            let id = ctx.params.id;
            // console.log(id);
            let deleteResult = await deleteShopbyId(id);
            if (deleteResult) {
                ctx.body = {
                    success: true,
                    data: { deleteResult: deleteResult },
                    info: { msg: "删除商品成功" },
                    code: 200
                }
            } else {
                ctx.body = {
                    success: false,
                    data: { deleteResult: deleteResult },
                    info: { msg: "删除商品失败" },
                    code: 200
                }
            }
        } catch (error) {
            ctx.body = {
                success: false,
                data: {},
                info: { msg: "服务器请求异常" },
                code: 500
            }
        }
    },
    updateShop: async (ctx, next) => {
        try {
            const { id, shopName, type, price } = ctx.request.body;
            console.log(id, shopName, type, price);
            let updateResult = await updateShopbyUserId(id, shopName, type, price);
            console.log("updateResult", updateResult);
            if (updateResult) {
                ctx.body = {
                    success: true,
                    data: { updateResult: updateResult },
                    info: { msg: "更新商品成功" },
                    code: 200
                }
            } else {
                ctx.body = {
                    success: false,
                    data: { updateResult: updateResult },
                    info: { msg: "更新商品失败" },
                    code: 200
                }
            }
        } catch (error) {
            ctx.body = {
                success: false,
                data: {},
                info: { msg: "服务器请求异常" },
                code: 500
            }
        }

    },
    downloadShopPic: async (ctx, next) => {
        try {
            let id = ctx.params.id;
            const downFileName = id + '.png'
            let filePath = path.join(process.cwd(),'./public/upload/shop/')+ `${downFileName}`;
            if (fs.existsSync(filePath)) {
                ctx.body = fs.createReadStream(filePath) //filePath 读出该文件的路径
            } else {
                ctx.body = {}
            }
        } catch (error) {
            console.log(error);
            ctx.body = {
                success: false,
                data: '',
                info: { msg: '下载商品图片失败' },
                code: 200
            }
        }

    }

}